﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETPerformance.App_BusinessLogic.BasePages;
using ASPNETPerformance.App_BusinessLogic.Helpers;
using ASPNETPerformance.App_BusinessLogic.StaticPage;
using ASPNETPerformance.App_BusinessLogic.Utils;

namespace ASPNETPerformance.UserControls {
    public partial class Part5 : BaseUserControl {
        protected void Page_Load( object sender, EventArgs e ) {
            if ( !this.Page.IsPostBack ) {
                BindPart5();
            }
        }

        private void BindPart5() {
            this.cbEnableDiskCache.Checked = CommonHelper.GetCookieBool( "IsEnableDiskCache" );
            this.cbEnableDiskCacheAndCompression.Checked = CommonHelper.GetCookieBool( "IsEnableDiskCacheAndCompression" );
            this.cbEnableDiskCacheAndOutputCache.Checked = CommonHelper.GetCookieBool( "IsEnableDiskCacheAndOutputCache" );
            this.txtDiskCacheTime.Text = CommonHelper.GetCookieInt( "DiskCacheTime" ).ToString();
        }

        protected void BtnPart5RemoveOutputCache_Click( object sender, EventArgs e ) {
            StaticPageManager.RemoveOutputCache();
            this.lblPart5Result.Text = "保存成功。";
        }

        protected void BtnPart5_Click( object sender, EventArgs e ) {
            SaveInfo();

            this.lblPart5Result.Text = "保存成功。";
        }

        public void SaveInfo() {
            CommonHelper.SetCookie( "IsEnableDiskCache",
                        this.cbEnableDiskCache.Checked.ToString(), new TimeSpan( 365, 0, 0, 0, 0 ) );

            CommonHelper.SetCookie( "IsEnableDiskCacheAndCompression",
                this.cbEnableDiskCacheAndCompression.Checked.ToString(), new TimeSpan( 365, 0, 0, 0, 0 ) );

            CommonHelper.SetCookie( "IsEnableDiskCacheAndOutputCache",
              this.cbEnableDiskCacheAndOutputCache.Checked.ToString(), new TimeSpan( 365, 0, 0, 0, 0 ) );

            CommonHelper.SetCookie( "DiskCacheTime",
              this.txtDiskCacheTime.Text, new TimeSpan( 365, 0, 0, 0, 0 ) );
        }

    }
}